<template>
  <div class="category" v-show="category">
    <div class="sprite_icon arrow"></div>
    <el-card class="songscategory">
      <div slot="header" class="clearfix">
        <button class="songscategory-select" @click="getCat('全部')">
          全部风格
        </button>
      </div>
      <div class="category-list">
        <dl
          v-for="(cat, index) in catlist"
          :key="index"
          :class="`item${index}`"
        >
          <dt>
            <i class="sprite_icon2"></i>
            {{ cat.categories }}
          </dt>
          <dd>
            <a
              :class="catItem.activity ? 'activity' : ''"
              v-for="catItem in cat.sub"
              :key="catItem.name"
              @click.stop="getCat(catItem.name, catItem)"
              href="##"
              >{{ catItem.name }}</a
            >
          </dd>
        </dl>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  name: 'SongsCategory',
  computed: {
    ...mapState('playlist', ['catlist'])
  },
  props: {
    category: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    ...mapActions('playlist', ['getCatlist', 'getSongslist']),
    // 获取当前点击的分类
    getCat(name, catItem) {
      // 控制activity，让单个a标签有个点击切换效果
      if (this.catItem && this.name !== name) {
        this.$set(this.catItem, 'activity', false)
      }
      // 如果点击的是相同的a标签，就不发请求
      if (!this.name || this.name !== name) {
        this.$set(catItem, 'activity', true)
        this.getSongslist(name)
        // 触发update事件，让其隐藏
        this.$emit('update:category', false)
      }
      this.catItem = catItem
      this.name = name
    }
  },
  mounted() {
    this.getCatlist()
    const name=this.$route.query.cat
    if(!name) return
    this.getSongslist(name)
    this.$emit('update:category', false)
   
  }
}
</script>

<style lang="less" scoped>
.category {
  position: absolute;
  top: 43px;
  margin-top: 5px;
  z-index: 5;
}
.songscategory {
  width: 674px;
  box-shadow: 0 4px 7px #555;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.9);
  .songscategory-select {
    cursor: pointer;
    line-height: 25px;
    font-size: 12px;
    padding: 0 10px;
    border-radius: 3px;
    background-color: #fafafa;
    border: 1px solid #d3d3d3;
  }
}
.category-list {
  margin-top: -20px;
  dl {
    display: flex;
    align-items: center;

    dt {
      display: flex;
      padding: 15px 0 10px;
      width: 70px;
      font-weight: bold;
      align-items: center;
      flex-shrink: 0;
      border-right: 1px solid #e2e2e2;
      i {
        display: inline-block;
        width: 24px;
        height: 24px;
        margin-right: 6px;
      }
    }
    dd {
      display: flex;
      padding-left: 10px;
      flex-wrap: wrap;

      a {
        margin-top: 10px;
        padding: 0 10px;
        flex-shrink: 0;
        border-right: 1px solid #ccc8c8;
      }
    }
  }

  .item0 {
    i {
      background-position: -20px -735px;
    }
  }
  .item1 {
    i {
      background-position: 0 -60px;
    }
  }

  .item2 {
    i {
      background-position: 0 -88px;
    }
  }

  .item3 {
    i {
      background-position: 0 -117px;
    }
  }

  .item4 {
    i {
      background-position: 0 -141px;
    }
  }
}
.arrow {
  position: absolute;
  top: -11px;
  left: 110px;
  width: 24px;
  height: 11px;
  background-position: -48px 0;
}
.activity {
  background: #bdbbbb;
  color: #fff;
}
</style>
